package com.lingnan.wanxiangxinxun.task;

import com.lingnan.wanxiangxinxun.constant.redisConstant;
import com.lingnan.wanxiangxinxun.entity.po.News;
import com.lingnan.wanxiangxinxun.service.INewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class getHotNewsTask {

    @Autowired
    private INewsService iNewsService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    // 缓存【为用户推荐新闻中的热门新闻】，不用每次都执行一次查询热门新闻的操作
    //@Scheduled(cron = "*/10 * * * * ?") // 每10秒触发(测试用)
    @Scheduled(cron = "5 */5 * * * ?") // 每五分钟的第5秒执行(避免整点雪崩)
    public void getHotNewsForPushNews() {
        List<News> hotNews = iNewsService.getHotNews(50);
        log.info("缓存热门新闻");
        //将热门新闻存入redis里面
        redisTemplate.opsForValue().set(redisConstant.HOT_NEWS_KEY, hotNews);
    }

}
